﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Tanmia.Core;
using System.Windows.Controls.Ribbon;
using Tanmia.Windows.ItemScenario;
using Tanmia.Windows.ColorScenario;
using Tanmia.Windows.PersonScenario;
using Tanmia.Windows.UnitScenario;
using Tanmia.Domain;

namespace Tanmia.Windows.DiscriminatedItemScenario
{
    /// <summary>
    /// Interaction logic for DiscriminatedItemEdit.xaml
    /// </summary>
    public partial class DiscriminatedItemEdit : RibbonWindow
    {
        DiscriminatedItemViewModel DiscriminatedItemVM = new DiscriminatedItemViewModel();

        DiscriminatedItemDomain discriminatedItemObj = new DiscriminatedItemDomain();


        public DiscriminatedItemEdit()
        {
            InitializeComponent();

            DataContext = DiscriminatedItemVM;
        }


        private void ShowValidationTool(string error)
        {
            vatError.Label = error;
            vatError.Visibility = Visibility.Visible;
        }

        private bool Save()
        {
            DiscriminatedItemVM.Entity.Validate();

            string err = DiscriminatedItemVM.Error;

            if (discriminatedItemObj.IsDiscriminatedItemExisting(cmbItem.SelectedItem as Item, cmbColor.SelectedItem as Tanmia.Core.Color, cmbManufacturer.SelectedItem as Person))
                if (txtDiscriminatedItemID.Text == "0")
                    if (!string.IsNullOrEmpty(err))
                        err += "\n\rالقطعة المميزة التي أدخلتها موجودة مسبقاً";
                    else
                        err += "القطعة المميزة التي أدخلتها موجودة مسبقاً";

            if (!string.IsNullOrEmpty(err))
            {
                ShowValidationTool(err);
                return false;
            }
            else
            {
                try
                {
                    DiscriminatedItemVM.Save();

                    if (vatError.Visibility == Visibility.Visible)
                        vatError.Visibility = Visibility.Collapsed;

                    return true;

                }
                catch (Exception ex)
                {
                    ShowValidationTool(ex.Message);
                    return false;
                }
            }
        }

        private bool easyAdd()
        {
            if (cmbColor.SelectedItem != Tanmia.Core.Color.EasyAdd && cmbManufacturer.SelectedItem != Person.EasyAdd)
            {
                ShowValidationTool("يجب أن تختار أحد الحقلين اللون أو المصنع أو كلاهما كحقل للحفظ السريع.");
                return false;
            }

            if (cmbUnit.SelectedItem == Unit.SelectUnit)
            {
                ShowValidationTool("يجب أن تختار وحدة القياس الإفتراضية.");
                return false;
            }

            if (cmbColor.SelectedItem == Tanmia.Core.Color.EasyAdd && cmbManufacturer.SelectedItem != Person.EasyAdd)
            {
                var win = new EasyAdd(cmbItem.SelectedItem as Item, cmbManufacturer.SelectedItem as Person, cmbUnit.SelectedItem as Unit);
                win.ShowInTaskbar = false;
                win.ShowDialog();
                return true;
            }

            if (cmbColor.SelectedItem != Tanmia.Core.Color.EasyAdd && cmbManufacturer.SelectedItem == Person.EasyAdd)
            {
                var win = new EasyAdd(cmbItem.SelectedItem as Item, cmbColor.SelectedItem as Tanmia.Core.Color, cmbUnit.SelectedItem as Unit);
                win.ShowInTaskbar = false;
                win.ShowDialog();
                return true;
            }
            
            if (cmbColor.SelectedItem == Tanmia.Core.Color.EasyAdd && cmbManufacturer.SelectedItem == Person.EasyAdd)
            {
                var win = new EasyAdd(cmbItem.SelectedItem as Item, cmbUnit.SelectedItem as Unit);
                win.ShowInTaskbar = false;
                win.ShowDialog();
                return true;
            }

            return true;
        }


        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbItem.SelectedIndex = 0;
            cmbManufacturer.SelectedIndex = 0;
            cmbUnit.SelectedIndex = 0;

            cmbRecord.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnSaveAndNew_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                btnClear_Click(sender, e);
        }

        private void btnEasyAdd_Click(object sender, RoutedEventArgs e)
        {
            easyAdd();
        }

        private void btnEasyAddAndNew_Click(object sender, RoutedEventArgs e)
        {
            if (easyAdd())
                btnClear_Click(sender, e);
        }

        private void btnSaveAndPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPerviewPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSetupPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            object selectedItem = cmbUnit.SelectedItem;

            DiscriminatedItemVM.Entity = new DiscriminatedItem();
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbItem.SelectedIndex = 0;
            cmbManufacturer.SelectedIndex = 0;
            cmbUnit.SelectedItem = selectedItem;

            if (vatError.Visibility == Visibility.Visible)
                vatError.Visibility = Visibility.Collapsed;
        }

        private void btnSaveAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                Close();
        }

        private void btnEasyAddAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (easyAdd())
                Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            DiscriminatedItemVM.Last();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            DiscriminatedItemVM.Next();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            DiscriminatedItemVM.Previous();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            DiscriminatedItemVM.First();
        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            WindowManager.ShowDialogEditor<ItemEdit>();
            DiscriminatedItemVM.UpdateItems();
        }

        private void btnAddColor_Click(object sender, RoutedEventArgs e)
        {
            WindowManager.ShowDialogEditor<ColorEdit>();
            DiscriminatedItemVM.UpdateColors((cmbItem.SelectedItem as Item).Category);
            cmbColor.SelectedIndex = 0;
        }

        private void btnAddManufacturer_Click(object sender, RoutedEventArgs e)
        {
            WindowManager.ShowDialogEditor<PersonEdit>();
            DiscriminatedItemVM.UpdateManufacturers();
        }

        private void btnAddUnit_Click(object sender, RoutedEventArgs e)
        {
            WindowManager.ShowDialogEditor<UnitEdit>();
            DiscriminatedItemVM.UpdateUnits();
        }

        private void cmbItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbItem.SelectedItem != null)
                DiscriminatedItemVM.UpdateColors((cmbItem.SelectedItem as Item).Category);
            cmbColor.SelectedIndex = 0;
        }

    }
}
